home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / WarpQuake / Src / r_draw.c < prev    next >
C/C++ Source or Header  |  2000-05-22  |  20KB  |  928 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20.  
  21. // r_draw.c
  22.  
  23. #include "quakedef.h"
  24. #include "r_local.h"
  25. #include "d_local.h"    // FIXME: shouldn't need to include this
  26.  
  27. #define MAXLEFTCLIPEDGES        100
  28.  
  29. // !!! if these are changed, they must be changed in asm_draw.h too !!!
  30. #define FULLY_CLIPPED_CACHED    0x80000000
  31. #define FRAMECOUNT_MASK            0x7FFFFFFF
  32.  
  33. unsigned int    cacheoffset;
  34.  
  35. int            c_faceclip;                    // number of faces clipped
  36.  
  37. zpointdesc_t    r_zpointdesc;
  38.  
  39. static polydesc_t        r_polydesc;
  40.  
  41.  
  42.  
  43. clipplane_t    *entity_clipplanes;
  44. clipplane_t    view_clipplanes[4];
  45. clipplane_t    world_clipplanes[16];
  46.  
  47. medge_t            *r_pedge;
  48.  
  49. static qboolean        r_leftclipped, r_rightclipped;
  50. static qboolean    makeleftedge, makerightedge;
  51. static qboolean        r_nearzionly;
  52.  
  53. int        sintable[SIN_BUFFER_SIZE];
  54. int        intsintable[SIN_BUFFER_SIZE];
  55.  
  56. static mvertex_t    r_leftenter, r_leftexit;
  57. static mvertex_t    r_rightenter, r_rightexit;
  58.  
  59. typedef struct
  60. {
  61.     float    u,v;
  62.     int        ceilv;
  63. } evert_t;
  64.  
  65. static int                r_emitted;
  66. static float            r_nearzi;
  67. static float            r_u1, r_v1, r_lzi1;
  68. static int                r_ceilv1;
  69.  
  70. static qboolean    r_lastvertvalid;
  71.  
  72.  
  73. #if    !id386
  74. //#if !id68k
  75.  
  76. /*
  77. ================
  78. R_EmitEdge
  79. ================
  80. */
  81. void R_EmitEdge (mvertex_t *pv0, mvertex_t *pv1)
  82. {
  83.     edge_t    *edge, *pcheck;
  84.     int        u_check;
  85.     float    u, u_step;
  86.     vec3_t    local, transformed;
  87.     float    *world;
  88.     int        v, v2, ceilv0;
  89.     float    scale, lzi0, u0, v0;
  90.     int        side;
  91.  
  92.     if (r_lastvertvalid)
  93.     {
  94.         u0 = r_u1;
  95.         v0 = r_v1;
  96.         lzi0 = r_lzi1;
  97.         ceilv0 = r_ceilv1;
  98.     }
  99.     else
  100.     {
  101.         world = &pv0->position[0];
  102.     
  103.     // transform and project
  104.         VectorSubtract (world, modelorg, local);
  105.         TransformVector (local, transformed);
  106.     
  107.         if (transformed[2] < NEAR_CLIP)
  108.             transformed[2] = NEAR_CLIP;
  109.     
  110.         lzi0 = 1.0 / transformed[2];
  111.     
  112.     // FIXME: build x/yscale into transform?
  113.         scale = xscale * lzi0;
  114.         u0 = (xcenter + scale*transformed[0]);
  115.         if (u0 < r_refdef.fvrectx_adj)
  116.             u0 = r_refdef.fvrectx_adj;
  117.         if (u0 > r_refdef.fvrectright_adj)
  118.             u0 = r_refdef.fvrectright_adj;
  119.     
  120.         scale = yscale * lzi0;
  121.         v0 = (ycenter - scale*transformed[1]);
  122.         if (v0 < r_refdef.fvrecty_adj)
  123.             v0 = r_refdef.fvrecty_adj;
  124.         if (v0 > r_refdef.fvrectbottom_adj)
  125.             v0 = r_refdef.fvrectbottom_adj;
  126.     
  127.         ceilv0 = (int) ceil(v0);
  128.     }
  129.  
  130.     world = &pv1->position[0];
  131.  
  132. // transform and project
  133.     VectorSubtract (world, modelorg, local);
  134.     TransformVector (local, transformed);
  135.  
  136.     if (transformed[2] < NEAR_CLIP)
  137.         transformed[2] = NEAR_CLIP;
  138.  
  139.     r_lzi1 = 1.0 / transformed[2];
  140.  
  141.     scale = xscale * r_lzi1;
  142.     r_u1 = (xcenter + scale*transformed[0]);
  143.     if (r_u1 < r_refdef.fvrectx_adj)
  144.         r_u1 = r_refdef.fvrectx_adj;
  145.     if (r_u1 > r_refdef.fvrectright_adj)
  146.         r_u1 = r_refdef.fvrectright_adj;
  147.  
  148.     scale = yscale * r_lzi1;
  149.     r_v1 = (ycenter - scale*transformed[1]);
  150.     if (r_v1 < r_refdef.fvrecty_adj)
  151.         r_v1 = r_refdef.fvrecty_adj;
  152.     if (r_v1 > r_refdef.fvrectbottom_adj)
  153.         r_v1 = r_refdef.fvrectbottom_adj;
  154.  
  155.     if (r_lzi1 > lzi0)
  156.         lzi0 = r_lzi1;
  157.  
  158.     if (lzi0 > r_nearzi)    // for mipmap finding
  159.         r_nearzi = lzi0;
  160.  
  161. // for right edges, all we want is the effect on 1/z
  162.     if (r_nearzionly)
  163.         return;
  164.  
  165.     r_emitted = 1;
  166.  
  167.     r_ceilv1 = (int) ceil(r_v1);
  168.  
  169.  
  170. // create the edge
  171.     if (ceilv0 == r_ceilv1)
  172.     {
  173.     // we cache unclipped horizontal edges as fully clipped
  174.         if (cacheoffset != 0x7FFFFFFF)
  175.         {
  176.             cacheoffset = FULLY_CLIPPED_CACHED |
  177.                     (r_framecount & FRAMECOUNT_MASK);
  178.         }
  179.  
  180.         return;        // horizontal edge
  181.     }
  182.  
  183.     side = ceilv0 > r_ceilv1;
  184.  
  185.     edge = edge_p++;
  186.  
  187.     edge->owner = r_pedge;
  188.  
  189.     edge->nearzi = lzi0;
  190.  
  191.     if (side == 0)
  192.     {
  193.     // trailing edge (go from p1 to p2)
  194.         v = ceilv0;
  195.         v2 = r_ceilv1 - 1;
  196.  
  197.         edge->surfs[0] = surface_p - surfaces;
  198.         edge->surfs[1] = 0;
  199.  
  200.         u_step = ((r_u1 - u0) / (r_v1 - v0));
  201.         u = u0 + ((float)v - v0) * u_step;
  202.     }
  203.     else
  204.     {
  205.     // leading edge (go from p2 to p1)
  206.         v2 = ceilv0 - 1;
  207.         v = r_ceilv1;
  208.  
  209.         edge->surfs[0] = 0;
  210.         edge->surfs[1] = surface_p - surfaces;
  211.  
  212.         u_step = ((u0 - r_u1) / (v0 - r_v1));
  213.         u = r_u1 + ((float)v - r_v1) * u_step;
  214.     }
  215.  
  216.     edge->u_step = u_step*0x100000;
  217.     edge->u = u*0x100000 + 0xFFFFF;
  218.  
  219. // we need to do this to avoid stepping off the edges if a very nearly
  220. // horizontal edge is less than epsilon above a scan, and numeric error causes
  221. // it to incorrectly extend to the scan, and the extension of the line goes off
  222. // the edge of the screen
  223. // FIXME: is this actually needed?
  224.     if (edge->u < r_refdef.vrect_x_adj_shift20)
  225.         edge->u = r_refdef.vrect_x_adj_shift20;
  226.     if (edge->u > r_refdef.vrectright_adj_shift20)
  227.         edge->u = r_refdef.vrectright_adj_shift20;
  228.  
  229. //
  230. // sort the edge in normally
  231. //
  232.     u_check = edge->u;
  233.     if (edge->surfs[0])
  234.         u_check++;    // sort trailers after leaders
  235.  
  236.     if (!newedges[v] || newedges[v]->u >= u_check)
  237.     {
  238.         edge->next = newedges[v];
  239.         newedges[v] = edge;
  240.     }
  241.     else
  242.     {
  243.         pcheck = newedges[v];
  244.         while (pcheck->next && pcheck->next->u < u_check)
  245.             pcheck = pcheck->next;
  246.         edge->next = pcheck->next;
  247.         pcheck->next = edge;
  248.     }
  249.  
  250.     edge->nextremove = removeedges[v2];
  251.     removeedges[v2] = edge;
  252. }
  253.  
  254. //#endif
  255.  
  256. //#if id68k
  257. //void __asm R_ClipEdge (register __a0 mvertex_t *pv0,
  258. //                       register __a1 mvertex_t *pv1,
  259. //                       register __a2 clipplane_t *clip);
  260. //#else
  261. /*
  262. ================
  263. R_ClipEdge
  264. ================
  265. */
  266. void R_ClipEdge (mvertex_t *pv0, mvertex_t *pv1, clipplane_t *clip)
  267. {
  268.     if (clip)
  269.     {
  270.         do
  271.         {
  272.             float d0, d1;
  273.  
  274.             d0 = DotProduct (pv0->position, clip->normal) - clip->dist;
  275.             d1 = DotProduct (pv1->position, clip->normal) - clip->dist;
  276.  
  277.             if (d0 >= 0)
  278.             {
  279.                 mvertex_t clipvert;
  280.  
  281.             // point 0 is unclipped
  282.                 if (d1 >= 0)
  283.                 {
  284.                 // both points are unclipped
  285.                     continue;
  286.                 }
  287.  
  288.             // only point 1 is clipped
  289.  
  290.             // we don't cache clipped edges
  291.                 cacheoffset = 0x7FFFFFFF;
  292.  
  293.                 {
  294.                     float f;
  295.  
  296.                     f = d0 / (d0 - d1);
  297.                     clipvert.position[0] = pv0->position[0] +
  298.                         f * (pv1->position[0] - pv0->position[0]);
  299.                     clipvert.position[1] = pv0->position[1] +
  300.                         f * (pv1->position[1] - pv0->position[1]);
  301.                     clipvert.position[2] = pv0->position[2] +
  302.                         f * (pv1->position[2] - pv0->position[2]);
  303.                 }
  304.  
  305.                 if (clip->leftedge)
  306.                 {
  307.                     r_leftclipped = true;
  308.                     r_leftexit = clipvert;
  309.                 }
  310.                 else if (clip->rightedge)
  311.                 {
  312.                     r_rightclipped = true;
  313.                     r_rightexit = clipvert;
  314.                 }
  315.  
  316.                 R_ClipEdge (pv0, &clipvert, clip->next);
  317.                 return;
  318.             }
  319.             else
  320.             {
  321.                 mvertex_t clipvert;
  322.  
  323.             // point 0 is clipped
  324.                 if (d1 < 0)
  325.                 {
  326.                 // both points are clipped
  327.                 // we do cache fully clipped edges
  328.                     if (!r_leftclipped)
  329.                         cacheoffset = FULLY_CLIPPED_CACHED |
  330.                                 (r_framecount & FRAMECOUNT_MASK);
  331.                     return;
  332.                 }
  333.  
  334.             // only point 0 is clipped
  335.                 r_lastvertvalid = false;
  336.  
  337.             // we don't cache partially clipped edges
  338.                 cacheoffset = 0x7FFFFFFF;
  339.  
  340.                 {
  341.                     float f;
  342.  
  343.                     f = d0 / (d0 - d1);
  344.                     clipvert.position[0] = pv0->position[0] +
  345.                         f * (pv1->position[0] - pv0->position[0]);
  346.                     clipvert.position[1] = pv0->position[1] +
  347.                         f * (pv1->position[1] - pv0->position[1]);
  348.                     clipvert.position[2] = pv0->position[2] +
  349.                         f * (pv1->position[2] - pv0->position[2]);
  350.                 }
  351.  
  352.                 if (clip->leftedge)
  353.                 {
  354.                     r_leftclipped = true;
  355.                     r_leftenter = clipvert;
  356.                 }
  357.                 else if (clip->rightedge)
  358.                 {
  359.                     r_rightclipped = true;
  360.                     r_rightenter = clipvert;
  361.                 }
  362.  
  363.                 R_ClipEdge (&clipvert, pv1, clip->next);
  364.                 return;
  365.             }
  366.         } while ((clip = clip->next) != NULL);
  367.     }
  368.  
  369. // add the edge
  370.     R_EmitEdge (pv0, pv1);
  371. }
  372.  
  373. //#endif
  374. #endif    // !id386
  375.  
  376.  
  377. /*
  378. ================
  379. R_EmitCachedEdge
  380. ================
  381. */
  382. void R_EmitCachedEdge (void)
  383. {
  384.     edge_t        *pedge_t;
  385.  
  386.     pedge_t = (edge_t *)((unsigned long)r_edges + r_pedge->cachededgeoffset);
  387.  
  388.     if (!pedge_t->surfs[0])
  389.         pedge_t->surfs[0] = surface_p - surfaces;
  390.     else
  391.         pedge_t->surfs[1] = surface_p - surfaces;
  392.  
  393.     if (pedge_t->nearzi > r_nearzi)    // for mipmap finding
  394.         r_nearzi = pedge_t->nearzi;
  395.  
  396.     r_emitted = 1;
  397. }
  398.  
  399.  
  400. /*
  401. ================
  402. R_RenderFace
  403. ================
  404. */
  405. void R_RenderFace (msurface_t *fa, int clipflags)
  406. {
  407.     int            i, lindex;
  408.     unsigned    mask;
  409.     mplane_t    *pplane;
  410.     float        distinv;
  411.     vec3_t        p_normal;
  412.     medge_t        *pedges, tedge;
  413.     clipplane_t    *pclip;
  414.  
  415. // skip out if no more surfs
  416.     if ((surface_p) >= surf_max)
  417.     {
  418.         r_outofsurfaces++;
  419.         return;
  420.     }
  421.  
  422. // ditto if not enough edges left, or switch to auxedges if possible
  423.     if ((edge_p + fa->numedges + 4) >= edge_max)
  424.     {
  425.         r_outofedges += fa->numedges;
  426.         return;
  427.     }
  428.  
  429.     c_faceclip++;
  430.  
  431. // set up clip planes
  432.     pclip = NULL;
  433.  
  434.     for (i=3, mask = 0x08 ; i>=0 ; i--, mask >>= 1)
  435.     {
  436.         if (clipflags & mask)
  437.         {
  438.             view_clipplanes[i].next = pclip;
  439.             pclip = &view_clipplanes[i];
  440.         }
  441.     }
  442.  
  443. // push the edges through
  444.     r_emitted = 0;
  445.     r_nearzi = 0;
  446.     r_nearzionly = false;
  447.     makeleftedge = makerightedge = false;
  448.     pedges = currententity->model->edges;
  449.     r_lastvertvalid = false;
  450.  
  451.     for (i=0 ; i<fa->numedges ; i++)
  452.     {
  453.         lindex = currententity->model->surfedges[fa->firstedge + i];
  454.  
  455.         if (lindex > 0)
  456.         {
  457.             r_pedge = &pedges[lindex];
  458.  
  459.         // if the edge is cached, we can just reuse the edge
  460.             if (!insubmodel)
  461.             {
  462.                 if (r_pedge->cachededgeoffset & FULLY_CLIPPED_CACHED)
  463.                 {
  464.                     if ((r_pedge->cachededgeoffset & FRAMECOUNT_MASK) ==
  465.                         r_framecount)
  466.                     {
  467.                         r_lastvertvalid = false;
  468.                         continue;
  469.                     }
  470.                 }
  471.                 else
  472.                 {
  473.                     if ((((unsigned long)edge_p - (unsigned long)r_edges) >
  474.                          r_pedge->cachededgeoffset) &&
  475.                         (((edge_t *)((unsigned long)r_edges +
  476.                          r_pedge->cachededgeoffset))->owner == r_pedge))
  477.                     {
  478.                         R_EmitCachedEdge ();
  479.                         r_lastvertvalid = false;
  480.                         continue;
  481.                     }
  482.                 }
  483.             }
  484.  
  485.         // assume it's cacheable
  486.             cacheoffset = (byte *)edge_p - (byte *)r_edges;
  487.             r_leftclipped = r_rightclipped = false;
  488.             R_ClipEdge (&r_pcurrentvertbase[r_pedge->v[0]],
  489.                         &r_pcurrentvertbase[r_pedge->v[1]],
  490.                         pclip);
  491.             r_pedge->cachededgeoffset = cacheoffset;
  492.  
  493.             if (r_leftclipped)
  494.                 makeleftedge = true;
  495.             if (r_rightclipped)
  496.                 makerightedge = true;
  497.             r_lastvertvalid = true;
  498.         }
  499.         else
  500.         {
  501.             lindex = -lindex;
  502.             r_pedge = &pedges[lindex];
  503.         // if the edge is cached, we can just reuse the edge
  504.             if (!insubmodel)
  505.             {
  506.                 if (r_pedge->cachededgeoffset & FULLY_CLIPPED_CACHED)
  507.                 {
  508.                     if ((r_pedge->cachededgeoffset & FRAMECOUNT_MASK) ==
  509.                         r_framecount)
  510.                     {
  511.                         r_lastvertvalid = false;
  512.                         continue;
  513.                     }
  514.                 }
  515.                 else
  516.                 {
  517.                 // it's cached if the cached edge is valid and is owned
  518.                 // by this medge_t
  519.                     if ((((unsigned long)edge_p - (unsigned long)r_edges) >
  520.                          r_pedge->cachededgeoffset) &&
  521.                         (((edge_t *)((unsigned long)r_edges +
  522.                          r_pedge->cachededgeoffset))->owner == r_pedge))
  523.                     {
  524.                         R_EmitCachedEdge ();
  525.                         r_lastvertvalid = false;
  526.                         continue;
  527.                     }
  528.                 }
  529.             }
  530.  
  531.         // assume it's cacheable
  532.             cacheoffset = (byte *)edge_p - (byte *)r_edges;
  533.             r_leftclipped = r_rightclipped = false;
  534.             R_ClipEdge (&r_pcurrentvertbase[r_pedge->v[1]],
  535.                         &r_pcurrentvertbase[r_pedge->v[0]],
  536.                         pclip);
  537.             r_pedge->cachededgeoffset = cacheoffset;
  538.  
  539.             if (r_leftclipped)
  540.                 makeleftedge = true;
  541.             if (r_rightclipped)
  542.                 makerightedge = true;
  543.             r_lastvertvalid = true;
  544.         }
  545.     }
  546.  
  547. // if there was a clip off the left edge, add that edge too
  548. // FIXME: faster to do in screen space?
  549. // FIXME: share clipped edges?
  550.     if (makeleftedge)
  551.     {
  552.         r_pedge = &tedge;
  553.         r_lastvertvalid = false;
  554.         R_ClipEdge (&r_leftexit, &r_leftenter, pclip->next);
  555.     }
  556.  
  557. // if there was a clip off the right edge, get the right r_nearzi
  558.     if (makerightedge)
  559.     {
  560.         r_pedge = &tedge;
  561.         r_lastvertvalid = false;
  562.         r_nearzionly = true;
  563.         R_ClipEdge (&r_rightexit, &r_rightenter, view_clipplanes[1].next);
  564.     }
  565.  
  566. // if no edges made it out, return without posting the surface
  567.     if (!r_emitted)
  568.         return;
  569.  
  570.     r_polycount++;
  571.  
  572.     surface_p->data = (void *)fa;
  573.     surface_p->nearzi = r_nearzi;
  574.     surface_p->flags = fa->flags;
  575.     surface_p->insubmodel = insubmodel;
  576.     surface_p->spanstate = 0;
  577.     surface_p->entity = currententity;
  578.     surface_p->key = r_currentkey++;
  579.     surface_p->spans = NULL;
  580.  
  581.     pplane = fa->plane;
  582. // FIXME: cache this?
  583.     TransformVector (pplane->normal, p_normal);
  584. // FIXME: cache this?
  585.     distinv = 1.0 / (pplane->dist - DotProduct (modelorg, pplane->normal));
  586.  
  587.     surface_p->d_zistepu = p_normal[0] * xscaleinv * distinv;
  588.     surface_p->d_zistepv = -p_normal[1] * yscaleinv * distinv;
  589.     surface_p->d_ziorigin = p_normal[2] * distinv -
  590.             xcenter * surface_p->d_zistepu -
  591.             ycenter * surface_p->d_zistepv;
  592.  
  593. //JDC    VectorCopy (r_worldmodelorg, surface_p->modelorg);
  594.     surface_p++;
  595. }
  596.  
  597.  
  598. /*
  599. ================
  600. R_RenderBmodelFace
  601. ================
  602. */
  603. void R_RenderBmodelFace (bedge_t *pedges, msurface_t *psurf)
  604. {
  605.     int            i;
  606.     unsigned    mask;
  607.     mplane_t    *pplane;
  608.     float        distinv;
  609.     vec3_t        p_normal;
  610.     medge_t        tedge;
  611.     clipplane_t    *pclip;
  612.  
  613. // skip out if no more surfs
  614.     if (surface_p >= surf_max)
  615.     {
  616.         r_outofsurfaces++;
  617.         return;
  618.     }
  619.  
  620. // ditto if not enough edges left, or switch to auxedges if possible
  621.     if ((edge_p + psurf->numedges + 4) >= edge_max)
  622.     {
  623.         r_outofedges += psurf->numedges;
  624.         return;
  625.     }
  626.  
  627.     c_faceclip++;
  628.  
  629. // this is a dummy to give the caching mechanism someplace to write to
  630.     r_pedge = &tedge;
  631.  
  632. // set up clip planes
  633.     pclip = NULL;
  634.  
  635.     for (i=3, mask = 0x08 ; i>=0 ; i--, mask >>= 1)
  636.     {
  637.         if (r_clipflags & mask)
  638.         {
  639.             view_clipplanes[i].next = pclip;
  640.             pclip = &view_clipplanes[i];
  641.         }
  642.     }
  643.  
  644. // push the edges through
  645.     r_emitted = 0;
  646.     r_nearzi = 0;
  647.     r_nearzionly = false;
  648.     makeleftedge = makerightedge = false;
  649. // FIXME: keep clipped bmodel edges in clockwise order so last vertex caching
  650. // can be used?
  651.     r_lastvertvalid = false;
  652.  
  653.     for ( ; pedges ; pedges = pedges->pnext)
  654.     {
  655.         r_leftclipped = r_rightclipped = false;
  656.         R_ClipEdge (pedges->v[0], pedges->v[1], pclip);
  657.  
  658.         if (r_leftclipped)
  659.             makeleftedge = true;
  660.         if (r_rightclipped)
  661.             makerightedge = true;
  662.     }
  663.  
  664. // if there was a clip off the left edge, add that edge too
  665. // FIXME: faster to do in screen space?
  666. // FIXME: share clipped edges?
  667.     if (makeleftedge)
  668.     {
  669.         r_pedge = &tedge;
  670.         R_ClipEdge (&r_leftexit, &r_leftenter, pclip->next);
  671.     }
  672.  
  673. // if there was a clip off the right edge, get the right r_nearzi
  674.     if (makerightedge)
  675.     {
  676.         r_pedge = &tedge;
  677.         r_nearzionly = true;
  678.         R_ClipEdge (&r_rightexit, &r_rightenter, view_clipplanes[1].next);
  679.     }
  680.  
  681. // if no edges made it out, return without posting the surface
  682.     if (!r_emitted)
  683.         return;
  684.  
  685.     r_polycount++;
  686.  
  687.     surface_p->data = (void *)psurf;
  688.     surface_p->nearzi = r_nearzi;
  689.     surface_p->flags = psurf->flags;
  690.     surface_p->insubmodel = true;
  691.     surface_p->spanstate = 0;
  692.     surface_p->entity = currententity;
  693.     surface_p->key = r_currentbkey;
  694.     surface_p->spans = NULL;
  695.  
  696.     pplane = psurf->plane;
  697. // FIXME: cache this?
  698.     TransformVector (pplane->normal, p_normal);
  699. // FIXME: cache this?
  700.     distinv = 1.0 / (pplane->dist - DotProduct (modelorg, pplane->normal));
  701.  
  702.     surface_p->d_zistepu = p_normal[0] * xscaleinv * distinv;
  703.     surface_p->d_zistepv = -p_normal[1] * yscaleinv * distinv;
  704.     surface_p->d_ziorigin = p_normal[2] * distinv -
  705.             xcenter * surface_p->d_zistepu -
  706.             ycenter * surface_p->d_zistepv;
  707.  
  708. //JDC    VectorCopy (r_worldmodelorg, surface_p->modelorg);
  709.     surface_p++;
  710. }
  711.  
  712.  
  713. /*
  714. ================
  715. R_RenderPoly
  716. ================
  717. */
  718. void R_RenderPoly (msurface_t *fa, int clipflags)
  719. {
  720.     int            i, lindex, lnumverts, s_axis, t_axis;
  721.     float        dist, lastdist, lzi, scale, u, v, frac;
  722.     unsigned    mask;
  723.     vec3_t        local, transformed;
  724.     clipplane_t    *pclip;
  725.     medge_t        *pedges;
  726.     mplane_t    *pplane;
  727.     mvertex_t    verts[2][100];    //FIXME: do real number
  728.     polyvert_t    pverts[100];    //FIXME: do real number, safely
  729.     int            vertpage, newverts, newpage, lastvert;
  730.     qboolean    visible;
  731.  
  732. // FIXME: clean this up and make it faster
  733. // FIXME: guard against running out of vertices
  734.  
  735.     s_axis = t_axis = 0;    // keep compiler happy
  736.  
  737. // set up clip planes
  738.     pclip = NULL;
  739.  
  740.     for (i=3, mask = 0x08 ; i>=0 ; i--, mask >>= 1)
  741.     {
  742.         if (clipflags & mask)
  743.         {
  744.             view_clipplanes[i].next = pclip;
  745.             pclip = &view_clipplanes[i];
  746.         }
  747.     }
  748.  
  749. // reconstruct the polygon
  750. // FIXME: these should be precalculated and loaded off disk
  751.     pedges = currententity->model->edges;
  752.     lnumverts = fa->numedges;
  753.     vertpage = 0;
  754.  
  755.     for (i=0 ; i<lnumverts ; i++)
  756.     {
  757.         lindex = currententity->model->surfedges[fa->firstedge + i];
  758.  
  759.         if (lindex > 0)
  760.         {
  761.             r_pedge = &pedges[lindex];
  762.             verts[0][i] = r_pcurrentvertbase[r_pedge->v[0]];
  763.         }
  764.         else
  765.         {
  766.             r_pedge = &pedges[-lindex];
  767.             verts[0][i] = r_pcurrentvertbase[r_pedge->v[1]];
  768.         }
  769.     }
  770.  
  771. // clip the polygon, done if not visible
  772.     while (pclip)
  773.     {
  774.         lastvert = lnumverts - 1;
  775.         lastdist = DotProduct (verts[vertpage][lastvert].position,
  776.                                pclip->normal) - pclip->dist;
  777.  
  778.         visible = false;
  779.         newverts = 0;
  780.         newpage = vertpage ^ 1;
  781.  
  782.         for (i=0 ; i<lnumverts ; i++)
  783.         {
  784.             dist = DotProduct (verts[vertpage][i].position, pclip->normal) -
  785.                     pclip->dist;
  786.  
  787.             if ((lastdist > 0) != (dist > 0))
  788.             {
  789.                 frac = dist / (dist - lastdist);
  790.                 verts[newpage][newverts].position[0] =
  791.                         verts[vertpage][i].position[0] +
  792.                         ((verts[vertpage][lastvert].position[0] -
  793.                           verts[vertpage][i].position[0]) * frac);
  794.                 verts[newpage][newverts].position[1] =
  795.                         verts[vertpage][i].position[1] +
  796.                         ((verts[vertpage][lastvert].position[1] -
  797.                           verts[vertpage][i].position[1]) * frac);
  798.                 verts[newpage][newverts].position[2] =
  799.                         verts[vertpage][i].position[2] +
  800.                         ((verts[vertpage][lastvert].position[2] -
  801.                           verts[vertpage][i].position[2]) * frac);
  802.                 newverts++;
  803.             }
  804.  
  805.             if (dist >= 0)
  806.             {
  807.                 verts[newpage][newverts] = verts[vertpage][i];
  808.                 newverts++;
  809.                 visible = true;
  810.             }
  811.  
  812.             lastvert = i;
  813.             lastdist = dist;
  814.         }
  815.  
  816.         if (!visible || (newverts < 3))
  817.             return;
  818.  
  819.         lnumverts = newverts;
  820.         vertpage ^= 1;
  821.         pclip = pclip->next;
  822.     }
  823.  
  824. // transform and project, remembering the z values at the vertices and
  825. // r_nearzi, and extract the s and t coordinates at the vertices
  826.     pplane = fa->plane;
  827.     switch (pplane->type)
  828.     {
  829.     case PLANE_X:
  830.     case PLANE_ANYX:
  831.         s_axis = 1;
  832.         t_axis = 2;
  833.         break;
  834.     case PLANE_Y:
  835.     case PLANE_ANYY:
  836.         s_axis = 0;
  837.         t_axis = 2;
  838.         break;
  839.     case PLANE_Z:
  840.     case PLANE_ANYZ:
  841.         s_axis = 0;
  842.         t_axis = 1;
  843.         break;
  844.     }
  845.  
  846.     r_nearzi = 0;
  847.  
  848.     for (i=0 ; i<lnumverts ; i++)
  849.     {
  850.     // transform and project
  851.         VectorSubtract (verts[vertpage][i].position, modelorg, local);
  852.         TransformVector (local, transformed);
  853.  
  854.         if (transformed[2] < NEAR_CLIP)
  855.             transformed[2] = NEAR_CLIP;
  856.  
  857.         lzi = 1.0 / transformed[2];
  858.  
  859.         if (lzi > r_nearzi)    // for mipmap finding
  860.             r_nearzi = lzi;
  861.  
  862.     // FIXME: build x/yscale into transform?
  863.         scale = xscale * lzi;
  864.         u = (xcenter + scale*transformed[0]);
  865.         if (u < r_refdef.fvrectx_adj)
  866.             u = r_refdef.fvrectx_adj;
  867.         if (u > r_refdef.fvrectright_adj)
  868.             u = r_refdef.fvrectright_adj;
  869.  
  870.         scale = yscale * lzi;
  871.         v = (ycenter - scale*transformed[1]);
  872.         if (v < r_refdef.fvrecty_adj)
  873.             v = r_refdef.fvrecty_adj;
  874.         if (v > r_refdef.fvrectbottom_adj)
  875.             v = r_refdef.fvrectbottom_adj;
  876.  
  877.         pverts[i].u = u;
  878.         pverts[i].v = v;
  879.         pverts[i].zi = lzi;
  880.         pverts[i].s = verts[vertpage][i].position[s_axis];
  881.         pverts[i].t = verts[vertpage][i].position[t_axis];
  882.     }
  883.  
  884. // build the polygon descriptor, including fa, r_nearzi, and u, v, s, t, and z
  885. // for each vertex
  886.     r_polydesc.numverts = lnumverts;
  887.     r_polydesc.nearzi = r_nearzi;
  888.     r_polydesc.pcurrentface = fa;
  889.     r_polydesc.pverts = pverts;
  890.  
  891. // draw the polygon
  892.     D_DrawPoly ();
  893. }
  894.  
  895.  
  896. /*
  897. ================
  898. R_ZDrawSubmodelPolys
  899. ================
  900. */
  901. void R_ZDrawSubmodelPolys (model_t *pmodel)
  902. {
  903.     int            i, numsurfaces;
  904.     msurface_t    *psurf;
  905.     float        dot;
  906.     mplane_t    *pplane;
  907.  
  908.     psurf = &pmodel->surfaces[pmodel->firstmodelsurface];
  909.     numsurfaces = pmodel->nummodelsurfaces;
  910.  
  911.     for (i=0 ; i<numsurfaces ; i++, psurf++)
  912.     {
  913.     // find which side of the node we are on
  914.         pplane = psurf->plane;
  915.  
  916.         dot = DotProduct (modelorg, pplane->normal) - pplane->dist;
  917.  
  918.     // draw the polygon
  919.         if (((psurf->flags & SURF_PLANEBACK) && (dot < -BACKFACE_EPSILON)) ||
  920.             (!(psurf->flags & SURF_PLANEBACK) && (dot > BACKFACE_EPSILON)))
  921.         {
  922.         // FIXME: use bounding-box-based frustum clipping info?
  923.             R_RenderPoly (psurf, 15);
  924.         }
  925.     }
  926. }
  927.  
  928.